//
//  LJLSubscribeVC.m
//  聆听FM
//
//  Created by dllo on 16/6/13.
//  Copyright © 2016年 Dark. All rights reserved.
//

#import "LJLSubscribeVC.h"
#import "LTDataBase.h"
#import "LTAlbumCollectTableViewCell.h"
#import "LTVoiceCollectTableViewCell.h"
#import "ZCSReHeaderTableViewCell.h"
#import "AlbumDetailViewController.h"
#import "ZCSListenListDetailViewController.h"
#import "DownSheet.h"


@interface LJLSubscribeVC ()<UITableViewDelegate, UITableViewDataSource, AlbumPro, VoicePro,DownSheetDelegate>

@property(nonatomic, strong) UITableView *subscribeTV;

@property (nonatomic, strong) NSMutableArray *albumArray;
@property (nonatomic, strong) NSMutableArray *voiceArray;

@property (nonatomic, retain) UIImageView *backImageView;

@property (nonatomic, retain) NSArray *sheetArray;

@property (nonatomic, assign) NSInteger  model_id;
@property (nonatomic, assign) NSInteger type;

@end

@implementation LJLSubscribeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"我的收藏";
    [self getData];
    [self createView];
    [self initSheetView];
}
- (void)initSheetView
{
    DownSheetModel *Model_1 = [[DownSheetModel alloc]init];
    Model_1.icon = @"deleta";
    Model_1.icon_on = @"deletaH";
    Model_1.title = @"取消收藏";
    DownSheetModel *Model_2 = [[DownSheetModel alloc]init];
    Model_2.icon = @"关闭zcs";
    Model_2.icon_on = @"关闭zcsh";
    Model_2.title = @"关闭";
    self.sheetArray = @[Model_1, Model_2];
}

- (void)createView
{
    self.subscribeTV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT) style:UITableViewStyleGrouped];
    self.subscribeTV.delegate = self;
    self.subscribeTV.dataSource = self;
    self.subscribeTV.sectionFooterHeight = 10;
    [self.view addSubview:_subscribeTV];
    
    [self.subscribeTV registerNib:[UINib nibWithNibName:@"LTAlbumCollectTableViewCell" bundle:nil] forCellReuseIdentifier:@"album"];
    [self.subscribeTV registerNib:[UINib nibWithNibName:@"LTVoiceCollectTableViewCell" bundle:nil] forCellReuseIdentifier:@"voice"];
    [self.subscribeTV registerNib:[UINib nibWithNibName:@"ZCSReHeaderTableViewCell" bundle:nil] forCellReuseIdentifier:@"reHeader"];
    
    self.backImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT - 49 - 64)];
    self.backImageView.image = [UIImage imageNamed:@"订阅"];
    self.backImageView.hidden = YES;
    [self.view addSubview:_backImageView];
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (self.albumArray.count == 0 && self.voiceArray.count == 0) {
        self.backImageView.hidden = NO;
        
        return 0;
    }
    if (self.albumArray.count == 0 || self.voiceArray.count == 0) {
        self.backImageView.hidden = YES;
        return 1;
    }
    self.backImageView.hidden = YES;
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.albumArray.count == 0 && self.voiceArray.count == 0) {
        return 0;
    }
    if (self.albumArray.count != 0 && self.voiceArray.count != 0) {
        if (section == 0) {
            return self.albumArray.count;
        }
        return self.voiceArray.count;
    }
    
    if (self.albumArray.count != 0 && self.voiceArray.count == 0) {
        return self.albumArray.count;
    }
    return self.voiceArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40 * FITHEIGHT;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (self.albumArray.count != 0 && self.voiceArray.count != 0) {
        if (indexPath.section == 0) {
            AlbumDetailViewController *album = [[AlbumDetailViewController alloc] init];
            LTAlbumModel *model = [self.albumArray objectAtIndex:indexPath.row];
            album.albmId = model.albumId;
            album.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:album animated:YES];
            return;
        }
        ZCSListenListDetailViewController *listen = [[ZCSListenListDetailViewController alloc] init];
        ZCSListenListDetailModel *model = [self.voiceArray objectAtIndex:indexPath.row];
        listen.ID = model.specialId;
        listen.type = [NSString stringWithFormat:@"%ld", model.contentType];
        listen.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:listen animated:YES];
        return;
    }
    
    if (self.albumArray.count != 0 && self.voiceArray.count == 0) {
        AlbumDetailViewController *album = [[AlbumDetailViewController alloc] init];
        LTAlbumModel *model = [self.albumArray objectAtIndex:indexPath.row];
        album.albmId = model.albumId;
        album.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:album animated:YES];
        return;
    }
    ZCSListenListDetailViewController *listen = [[ZCSListenListDetailViewController alloc] init];
    ZCSListenListDetailModel *model = [self.voiceArray objectAtIndex:indexPath.row];
    listen.ID = model.specialId;
    listen.type = [NSString stringWithFormat:@"%ld", model.contentType];
    listen.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:listen animated:YES];
    return;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (self.albumArray.count != 0 && self.voiceArray.count != 0) {
        if (section == 0) {
            ZCSReHeaderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reHeader"];
            cell.headerLabel.text = @"您订阅的专辑";
            cell.backgroundColor = [UIColor whiteColor];
            cell.headerLabel.textColor = [UIColor blackColor];
            cell.moreImage.hidden = YES;
            cell.moreLabel.hidden = YES;
            return cell;
        }
        ZCSReHeaderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reHeader"];
        cell.headerLabel.text = @"您订阅的听单";
        cell.backgroundColor = [UIColor whiteColor];
        cell.headerLabel.textColor = [UIColor blackColor];
        cell.moreImage.hidden = YES;
        cell.moreLabel.hidden = YES;
        return cell;
    }
    if (self.albumArray.count != 0 && self.voiceArray.count == 0) {
        ZCSReHeaderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reHeader"];
        cell.headerLabel.text = @"您订阅的专辑";
        cell.backgroundColor = [UIColor whiteColor];
        cell.headerLabel.textColor = [UIColor blackColor];
        cell.moreImage.hidden = YES;
        cell.moreLabel.hidden = YES;
        return cell;
    }
    ZCSReHeaderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reHeader"];
    cell.headerLabel.text = @"您订阅的听单";
    cell.backgroundColor = [UIColor whiteColor];
    cell.headerLabel.textColor = [UIColor blackColor];
    cell.moreImage.hidden = YES;
    cell.moreLabel.hidden = YES;
    return cell;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (self.albumArray.count != 0 && self.voiceArray.count != 0) {
        if (indexPath.section == 0) {
            LTAlbumCollectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"album"];
            cell.delegate = self;
            cell.model = [self.albumArray objectAtIndex:indexPath.row];
            return cell;
        }
        LTVoiceCollectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"voice"];
        cell.model = [self.voiceArray objectAtIndex:indexPath.row];
        cell.delegate = self;
        return cell;
    }
    
    if (self.albumArray.count != 0 && self.voiceArray.count == 0) {
        LTAlbumCollectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"album"];
        cell.model = [self.albumArray objectAtIndex:indexPath.row];
        cell.delegate = self;
        return cell;
    }
    LTVoiceCollectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"voice"];
    cell.model = [self.voiceArray objectAtIndex:indexPath.row];
    cell.delegate = self;
    return cell;
    
}

- (void)pushList:(NSInteger)model_id
{
    [self showAlretViewWith:model_id Type:1];
}
- (void)showAlretViewWith:(NSInteger)model_id Type:(NSInteger)type
{
    self.model_id = model_id;
    self.type = type;
    DownSheet *sheet = [[DownSheet alloc]initWithlist:self.sheetArray height:10];
    sheet.delegate = self;
    [sheet showInView:nil];
}
- (void)didSelectIndex:(NSInteger)index{
    if (index == 0) {
        LTDataBase *data = [LTDataBase shareLTDataBaseTool];
        [data delegateDataWithModel_id:self.model_id type:self.type];
        [self getData];
    }
    
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = NO;
    [self getData];
}
-(void)viewWillDisappear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = NO;
}




- (void)pushAlbumList:(NSInteger)albumId
{
    [self showAlretViewWith:albumId Type:0];
   
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80 * FITHEIGHT;
}


- (void)getData
{
    LTDataBase *data = [LTDataBase shareLTDataBaseTool];
    self.albumArray = [data selectAllWithtype:0];
    
    self.voiceArray = [data selectAllWithtype:1];
    [self.subscribeTV reloadData];
    
  
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
